﻿using System;
using System.Net;
using System.Threading;
using Platformer_Example.Content.Images.Environment;
using Platformer_Example.Content.Images.Player;
using Platformer_Example.Controller.Input;
using Platformer_Example.Controller.Map;
using Platformer_Example.Controller.Network;
using Platformer_Example.Controller.Network.Messages;
using Platformer_Example.Screen.Common;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Input;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Platformer_Example.Screen.LoadGame
{
    public class LoadGame : BaseScreen
    {
        private readonly NetworkClient _client;
        private readonly ClientData _clientData;
        private readonly NetworkServer _server;
        private readonly ServerData _serverData;

        private MapData _map;

        public LoadGame(NetworkServer server, ServerData serverData, Controller.Network.Messages.LoadGame gameData)
        {
            _server = server;
            _serverData = serverData;
            _server.OnLoadingDone += OnServerLoadingDone;
            PrepareLoadGame();
            new Thread(() => LoadMap(gameData, _serverData.LocalPlayer)).Start();
        }

        public LoadGame(NetworkClient client, ClientData clientData, Controller.Network.Messages.LoadGame gameData)
        {
            _client = client;
            _clientData = clientData;
            _client.OnPrepareForPlaying += OnClientPrepareForPlaying;
            PrepareLoadGame();
            new Thread(() => LoadMap(gameData, _clientData.Id)).Start();
        }

        public override void UnloadContent()
        {
            if (_server != null)
            {
                _server.OnLoadingDone -= OnServerLoadingDone;
            }
            else
            {
                _client.OnPrepareForPlaying -= OnClientPrepareForPlaying;
            }
            base.UnloadContent();
        }

        void PrepareLoadGame()
        {
            SpriteSheet.UnloadContent();
            SpritesheetDefaultFont.LoadContent();
            SpritesheetPlayer.LoadContent();
            SpritesheetCastle.LoadContent();
            SpritesheetObjects.LoadContent();
        }

        private void OnClientPrepareForPlaying(PrepareForPlaying message, IPAddress senderaddress)
        {
            PrepareForPlaying();
        }

        private void OnServerLoadingDone(LoadingDone message, IPAddress senderaddress)
        {
            if (_serverData.LoadingDone(Guid.Parse(message.Client)))
                PrepareForPlaying();
        }

        private void LoadMap(Controller.Network.Messages.LoadGame data, Guid localGuid)
        {
            _map = new MapData(data, localGuid);
            System.Threading.Thread.Sleep(1000);

            if (_client != null)
            {
                _client.SendLoadingDone(_clientData.ServerIPAddress, localGuid.ToString());
            }
            else
            {
                if(_serverData.LoadingDone(localGuid))
                    PrepareForPlaying();
            }
        }

        private void PrepareForPlaying()
        {
            if (_server != null)
            {
                ChangeScreen(new Playscreen.Playscreen(_server, _serverData, _map));
                _server.SendPrepareForPlaying(_serverData.ClientAddresses());
            }
            else
            {
                ChangeScreen(new Playscreen.Playscreen(_client, _clientData, _map));
            }
        }

        public override void LoadContent()
        {
            AddChildren(new Label(SpritesheetDefaultFont.GetSprite, "loading...")
                            {
                                PositionGridBound = Game.Options.ScreenSize/2,
                                TextAlign = TE_Rectangle.Alignment.CenterMiddle,
                                Scale = new TE_Vector2(3, 3)
                            });

            base.LoadContent();
        }

        public override void Keypressed(InputState.Key key)
        {
        }

        public override void KeyReleased(InputState.Key key)
        {
        }

        public override void InvalidKeyPressed(KeyboardInput.Key keyCode)
        {
        }
    }
}